<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
          "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta charset="utf-8">
<title>QUnit for ASN1 Encoder Class 'asn1.js'</title>
<script type="text/javascript" src="jquery-1.4.2.min.js"></script>
<script type="text/javascript" src="qunit.js"></script>
<link rel="stylesheet" href="qunit.css" type="text/css" media="screen" />

<!-- getPEM -->
<script src="../ext/yahoo.js"></script>
<script src="../ext/jsbn.js"></script>
<script src="../ext/jsbn2.js"></script>
<script src="../ext/base64.js"></script>
<script src="../src/base64x-1.1.js"></script>
<script src="../src/asn1-1.0.js"></script>
<script src="../src/asn1x509-1.0.js"></script>

<script type="text/javascript">
<!--
$(document).ready(function(){

// ======= TEST ===============================================================
// z2 self
  var sCer1PEM = "" +
"-----BEGIN CERTIFICATE-----\r\n" + 
"MIIBqDCCAVKgAwIBAgIJAJ53Xn2fQ6vJMA0GCSqGSIb3DQEBBQUAMBoxCzAJBgNV\r\n" + 
"BAYTAkpQMQswCQYDVQQKEwJ6MjAeFw0xMDA1MzEwNjE3NTZaFw0yMDA1MjgwNjE3\r\n" + 
"NTZaMBoxCzAJBgNVBAYTAkpQMQswCQYDVQQKEwJ6MjBcMA0GCSqGSIb3DQEBAQUA\r\n" + 
"A0sAMEgCQQC5sAMpUF/i7WDsVondzfW5TQWrQW4WQ7HKP5b8Ry7rH2LtR2iX8Vmu\r\n" + 
"mLfzrezseXHetfZNmiWQHE3HpJdy5aArAgMBAAGjezB5MB0GA1UdDgQWBBS7MIUo\r\n" + 
"gYmDIf2DOqOcwYCKCb3GjDBKBgNVHSMEQzBBgBS7MIUogYmDIf2DOqOcwYCKCb3G\r\n" + 
"jKEepBwwGjELMAkGA1UEBhMCSlAxCzAJBgNVBAoTAnoyggkAnndefZ9Dq8kwDAYD\r\n" + 
"VR0TBAUwAwEB/zANBgkqhkiG9w0BAQUFAANBAFhZ88q7ZS7THEcPJ8ogQ+78nyAn\r\n" + 
"DN7F1FsLp7jE9oM3kYQl6KYy0pwch6Wd6ypW86pel4JFOEr+LnVGODaoxFE=\r\n" + 
    "-----END CERTIFICATE-----\r\n";

  var hCer1 = "308201a830820152a0030201020209009e775e7d9f43abc9300d06092a864886f70d0101050500301a310b3009060355040613024a50310b3009060355040a13027a32301e170d3130303533313036313735365a170d3230303532383036313735365a301a310b3009060355040613024a50310b3009060355040a13027a32305c300d06092a864886f70d0101010500034b003048024100b9b00329505fe2ed60ec5689ddcdf5b94d05ab416e1643b1ca3f96fc472eeb1f62ed476897f159ae98b7f3adecec7971deb5f64d9a25901c4dc7a49772e5a02b0203010001a37b3079301d0603551d0e04160414bb30852881898321fd833aa39cc1808a09bdc68c304a0603551d23044330418014bb30852881898321fd833aa39cc1808a09bdc68ca11ea41c301a310b3009060355040613024a50310b3009060355040a13027a328209009e775e7d9f43abc9300c0603551d13040530030101ff300d06092a864886f70d01010505000341005859f3cabb652ed31c470f27ca2043eefc9f20270cdec5d45b0ba7b8c4f68337918425e8a632d29c1c87a59deb2a56f3aa5e978245384afe2e75463836a8c451";

test("Boolean Test", function() {
  var d = new KJUR.asn1.DERBoolean();
  expect(1);
  equal(d.getEncodedHex(), "0101ff", "Boolean TRUE");
});

test("Integer Test", function() {
  var d = new KJUR.asn1.DERInteger();
  d.setByBigInteger(new BigInteger("3", 16));
  equal(d.getEncodedHex(), "020103", "setByBigInteger(BI('3',16))");
  d.setByBigInteger(new BigInteger("3f", 16));
  equal(d.getEncodedHex(), "02013f", "setByBigInteger(BI('3f',16))");
  d.setByBigInteger(new BigInteger("254", 10));
  equal(d.getEncodedHex(), "020200fe", "setByBigInteger(BI('254',10))");
  d.setByBigInteger(new BigInteger("-3", 16));
  equal(d.getEncodedHex(), "0201fd", "setByBigInteger(BI('-3',16))");
  d.setByBigInteger(new BigInteger("-1", 16));
  equal(d.getEncodedHex(), "0201ff", "setByBigInteger(BI('-1',16))");
  d.setByInteger(-3);
  equal(d.getEncodedHex(), "0201fd", "setByInteger(-3)");
  var d2 = new KJUR.asn1.DERInteger({'bigint': new BigInteger("-3", 16)});
  equal(d2.getEncodedHex(), "0201fd", "constructor bigint -3");
  var d3 = new KJUR.asn1.DERInteger({'int': -3});
  equal(d3.getEncodedHex(), "0201fd", "constructor int -3");
  var d4 = new KJUR.asn1.DERInteger({'hex': 'fd'});
  equal(d4.getEncodedHex(), "0201fd", "constructor hex fd");
  var d5 = new KJUR.asn1.DERInteger({'hex': '00fd'});
  equal(d5.getEncodedHex(), "020200fd", "constructor hex 00fd");
});

test("BitString Test", function() {
  var d = new KJUR.asn1.DERBitString();
  d.setHexValueIncludingUnusedBits("04bac0");
  equal(d.getEncodedHex(), "030304bac0", "setHexValueIncludingUnusedBits > ub=4, bac0");
  d.setUnusedBitsAndHexValue(5, "bad0");
  equal(d.getEncodedHex(), "030305bad0", "setUnusedBitsAndHexValue > ub=5, bad0");
  d.setByBinaryString('1011');
  equal(d.getEncodedHex(), "030204b0", "setByBinaryString 1011 > ub=4, b0"); 

  d.setByBinaryString('1011000');
  equal(d.getEncodedHex(), "030204b0", "setByBinaryString 1011000 > ub=4, b0"); 

  d.setByBinaryString('11111111');
  equal(d.getEncodedHex(), "030200ff", "setByBinaryString 1(8) > ub=0, ff"); 

  d.setByBinaryString('111111111');
  equal(d.getEncodedHex(), "030307ff80", "setByBinaryString 1(9) > ub=7, ff80"); 
  d.setByBooleanArray([true, false, true, true, false]);
  equal(d.getEncodedHex(), "030204b0", "setByBooleanArray([t,f,t,t,f]) > ub=4, b0"); 
  var d2 = new KJUR.asn1.DERBitString({"hex":"04bac0"});
  equal(d2.getEncodedHex(), "030304bac0", "constructor hex 04bac0");
  var d3 = new KJUR.asn1.DERBitString({"bin":"1011"});
  equal(d3.getEncodedHex(), "030204b0", "constructor bin 1011"); 
  var d4 = new KJUR.asn1.DERBitString({"array": [true,false,true,true]});
  equal(d4.getEncodedHex(), "030204b0", "constructor array [t,f,t,t]"); 
  var d5 = new KJUR.asn1.DERBitString({obj:{seq: [{int: 3},{prnstr: {str: "aaa"}}]}});
  equal(d5.getEncodedHex(), "030b0030080201031303616161", "constructor params asn1");
});

test("OctetString Test", function() {
  var d = new KJUR.asn1.DEROctetString();
  d.setString('aaa');
  equal(d.getEncodedHex(), "0403616161", "OctetString aaa");
  d.setStringHex('000102');
  equal(d.getEncodedHex(), "0403000102", "OctetString x000102");
  var d2 = new KJUR.asn1.DEROctetString({hex:'010203'});
  equal(d2.getEncodedHex(), "0403010203", "constructor params hex");
  var d3 = new KJUR.asn1.DEROctetString({str:'aaa'});
  equal(d3.getEncodedHex(), "0403616161", "constructor params str aaa");
  var d4 = new KJUR.asn1.DEROctetString({obj:{seq: [{int: 3},{prnstr: {str: "aaa"}}]}});
  equal(d4.getEncodedHex(), "040a30080201031303616161", "constructor params asn1");
});

test("Null Test", function() {
  var d = new KJUR.asn1.DERNull();
  expect(1);
  equal(d.getEncodedHex(), "0500", "Null");
});

test("ObjectIdentifier Test", function() {
  var d = new KJUR.asn1.DERObjectIdentifier();

  expect(5);
  d.setValueHex('550406');
  equal(d.getEncodedHex(), "0603550406", "setValueHex('550406') C:2.5.4.6");
  d.setValueOidString('2.5.4.6');
  equal(d.getEncodedHex(), "0603550406", "setValueOidString('2.5.4.6') C:2.5.4.6");
  d.setValueOidString('1.2.34.56789');
  equal(d.getEncodedHex(), "06052a2283bb55", "setValueOidString('1.2.34.56789')");
  var d4 = new KJUR.asn1.DERObjectIdentifier({'oid':'2.5.4.6'});
  equal(d4.getEncodedHex(), "0603550406", "constructor oid 2.5.4.6");

  var d5 = new KJUR.asn1.DERObjectIdentifier({'oid':'0.1.1.2.2'});
  equal(d5.getEncodedHex(), "060401010202", "oid 0.1.1.2.2");
});

test("UTF8String Test", function() {
  var d = new KJUR.asn1.DERUTF8String();
  d.setString('aaa');
  equal(d.getEncodedHex(), "0c03616161", "UTF8String aaa");
  d.setString('bbb');
  equal(d.getEncodedHex(), "0c03626262", "UTF8String bbb");
  d.setString('あ');
  equal(d.getEncodedHex(), "0c03e38182", "UTF8String あ");
});

test("NumericString Test", function() {
  var d = new KJUR.asn1.DERNumericString();
  d.setString('aaa');
  equal(d.getEncodedHex(), "1203616161", "NumericString aaa");
  d.setString('bbb');
  equal(d.getEncodedHex(), "1203626262", "NumericString bbb");
});

test("PrintableString Test", function() {
  var d = new KJUR.asn1.DERPrintableString();
  expect(2);
  d.setString('aaa');
  equal(d.getEncodedHex(), "1303616161", "PrintableString aaa");
  d.setString('bbb');
  equal(d.getEncodedHex(), "1303626262", "PrintableString bbb");
});

test("TeletexString Test", function() {
  var d = new KJUR.asn1.DERTeletexString();
  expect(2);
  d.setString('aaa');
  equal(d.getEncodedHex(), "1403616161", "TeletexString aaa");
  d.setString('bbb');
  equal(d.getEncodedHex(), "1403626262", "TeletexString bbb");
});

test("IA5String Test", function() {
  var d = new KJUR.asn1.DERIA5String();
  d.setString('aaa');
  equal(d.getEncodedHex(), "1603616161", "IA5String aaa");
  d.setString('bbb');
  equal(d.getEncodedHex(), "1603626262", "IA5String bbb");
  d.setString('あ');
  equal(d.getEncodedHex(), "1603e38182", "IA5String あ");
});

test("UTCTime Test", function() {
  expect(5);

  var d1 = new KJUR.asn1.DERUTCTime();
  d1.setString('130430125959Z');
  equal(d1.getEncodedHex(), "170d3133303433303132353935395a", "setString 130430125959Z");

  var d2 = new KJUR.asn1.DERUTCTime();
  d2.setByDate(new Date(Date.UTC(2015, 0, 31, 0, 0, 0, 0)));
  equal(d2.s, "150131000000Z", "setByDate Date.UTC");

  var d3 = new KJUR.asn1.DERUTCTime();
  d3.setByDateValue(2015, 1, 31, 0, 0, 0);
  equal(d3.s, "150131000000Z", "setByDateValue");

  var d4 = new KJUR.asn1.DERUTCTime({'date':new Date(Date.UTC(2015, 0, 31, 0, 0, 0, 0))});
  equal(d4.s, "150131000000Z", "constructor date argument");

  var d5 = new KJUR.asn1.DERUTCTime();
  var d5hex = d5.getEncodedHex()
  equal(d5hex.substr(0, 4), "170d", "now[0,4]=170d");
});

test("GeneralizedTime Test", function() {
  expect(7);
  var d1 = new KJUR.asn1.DERGeneralizedTime();
  d1.setString('20130430125959Z');
  equal(d1.getEncodedHex(), "180f32303133303433303132353935395a", 
         "GeneralizedTime 20130430125959Z");

  var d2 = new KJUR.asn1.DERGeneralizedTime();
  d2.setByDate(new Date(Date.UTC(2015, 0, 31, 0, 0, 0, 0)));
  equal(d2.s, "20150131000000Z", "setByDate Date.UTC");

  var d3 = new KJUR.asn1.DERGeneralizedTime();
  d3.setByDateValue(2015, 1, 31, 0, 0, 0);
  equal(d3.s, "20150131000000Z", "setByDateValue");

  var date4 = new Date(Date.UTC(2015, 0, 31, 0, 0, 0, 0));
  var d4 = new KJUR.asn1.DERGeneralizedTime({'date': date4});
  equal(d4.s, "20150131000000Z", "constructor date argument");

  var d5 = new KJUR.asn1.DERGeneralizedTime();
  var d5hex = d5.getEncodedHex()
  equal(d5hex.substr(0, 4), "180f", "now[0,4]=180f");
  equal(d5.s.length, 15, "now length=15");

  var d6 = new KJUR.asn1.DERGeneralizedTime({millis: true});
  var d6hex = d6.getEncodedHex()
  equal(d6.s.match(/^[0-9]+[.]?[0-9]*Z$/) != null, true, "now ({millis: true}) match/^d+.?d*Z$/");

});

test("Sequence Test", function() {
  expect(4);
  var d1a = new KJUR.asn1.DERUTF8String();
  d1a.setString('a');
  var d1b = new KJUR.asn1.DERUTF8String();
  d1b.setString('b');
  var d1 = new KJUR.asn1.DERSequence();
  d1.setByASN1ObjectArray([d1a, d1b]);
  equal(d1.getEncodedHex(), "30060c01610c0162", "setByASN1ObjectArray");

  var d2a = new KJUR.asn1.DERUTF8String({'str':'a'});
  var d2b = new KJUR.asn1.DERUTF8String({'str':'b'});
  var d2 = new KJUR.asn1.DERSequence({'array': [d2a, d2b]});
  equal(d2.getEncodedHex(), "30060c01610c0162", "constructor array");

  var d3 = new KJUR.asn1.DERSequence({'array': []});
  equal(d3.getEncodedHex(), "3000", "constructor array []");

  var d4 = new KJUR.asn1.DERSequence();
  equal(d4.getEncodedHex(), "3000", "constructor array no initialize");
});

test("Set Test", function() {
  expect(2);
  var d1a = new KJUR.asn1.DERUTF8String();
  d1a.setString('a');
  var d1b = new KJUR.asn1.DERUTF8String();
  d1b.setString('b');
  var d1 = new KJUR.asn1.DERSet();
  d1.setByASN1ObjectArray([d1a, d1b]);
  equal(d1.getEncodedHex(), "31060c01610c0162", "setByASN1ObjectArray");

  var d2a = new KJUR.asn1.DERUTF8String({'str':'a'});
  var d2b = new KJUR.asn1.DERUTF8String({'str':'b'});
  var d2 = new KJUR.asn1.DERSet({'array': [d2a, d2b]});
  equal(d2.getEncodedHex(), "31060c01610c0162", "constructor array");
});

test("TaggedObject Test", function() {
  expect(4);

  var d1a = new KJUR.asn1.DERUTF8String();
  d1a.setString('a'); // 0c0161
  var d1 = new KJUR.asn1.DERTaggedObject();
  d1.setASN1Object(true, "a0", d1a);
  equal(d1.getEncodedHex(), "a0030c0161", "setASN1Object");

  var d2a = new KJUR.asn1.DERUTF8String({'str':'a'});
  var d2 = new KJUR.asn1.DERTaggedObject({'explicit':true,'tag':'a1','obj':d2a});
  equal(d2.getEncodedHex(), "a1030c0161", "constructor with params");

  var d3a = new KJUR.asn1.DERUTF8String({'str':'a'});
  var d3 = new KJUR.asn1.DERTaggedObject({'explicit':false,'tag':'a1','obj':d2a});
  equal(d3.getEncodedHex(), "a10161", "constructor with params implicit");

  var d4a = new KJUR.asn1.DERUTF8String({'str':'a'});
  var d4 = new KJUR.asn1.DERTaggedObject({'obj':d2a});
  equal(d4.getEncodedHex(), "a0030c0161", "constructor with defaults");
});

test("ASN1Util.getPEMStringFromHex", function() {
  var s = "" +
"-----BEGIN HOGE HOGE-----\r\n" +
"YWFh\r\n" +
"-----END HOGE HOGE-----\r\n";

  equal(KJUR.asn1.ASN1Util.getPEMStringFromHex("616161", "HOGE HOGE"), s, "aaa");
  equal(KJUR.asn1.ASN1Util.getPEMStringFromHex(hCer1, "CERTIFICATE"), sCer1PEM, "z2 cer1 pem");
});

});
-->
</script>
  
</head>
<body>
<div id="qunit"></div>
<div id="qunit-fixture">test markup</div>
<a href="index.html">TEST INDEX</a> | 
<a href="../api/index.html">API</a> | 
<a href="qunit-do-x509-hex2dn.html">x509-hex2dn</a> |
<a href="qunit-do-asn1x509.html">asn1x509</a> |
<a href="qunit-do-asn1x509-multirdn.html">asn1x509-multirdn</a> |
</body>
</html>
